//
//  AddViewController.m
//  CoreDataAddressBook
//
//  Created by dllo on 15/11/23.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "AddViewController.h"
#import "LTView.h"
#import "CoreDataManager.h"
#import "Student.h"
@interface AddViewController ()
@property(nonatomic, strong)CoreDataManager *manager;
@property(nonatomic, strong)LTView *nameView;
@property(nonatomic, strong)LTView *sexView;
@property(nonatomic, strong)LTView *numberView;
@property(nonatomic, strong)LTView *phoneView;
@property(nonatomic, strong)LTView *QQView;
@property(nonatomic, strong)LTView *addressView;
@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor cyanColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemDone) target:self action:@selector(finish:)];
    
    self.manager = [CoreDataManager defaults];
    NSLog(@"%@", [self.manager applicationDocumentsDirectory]);
    
    self.nameView = [[LTView alloc] initWithFrame:CGRectMake(10, 64, 350, 40)];
    [self.view addSubview:self.nameView];
    self.nameView.myLabel.text = @"姓名:";
    //    self.nameView.myTextField.text = self.name;
    
    self.sexView = [[LTView alloc] initWithFrame:CGRectMake(10, 120, 350, 40)];
    [self.view addSubview:self.sexView];
    self.sexView.myLabel.text = @"性别:";
    //    self.sexView.myTextField.text = self.sex;
    
    self.numberView = [[LTView alloc] initWithFrame:CGRectMake(10, 180, 350, 40)];
    [self.view addSubview:self.numberView];
    self.numberView.myLabel.text = @"学号:";
    //    self.numberView.myTextField.text = self.number.stringValue;
    
    self.phoneView = [[LTView alloc] initWithFrame:CGRectMake(10, 240, 350, 40)];
    [self.view addSubview:self.phoneView];
    self.phoneView.myLabel.text = @"电话:";
    //    self.phoneView.myTextField.text = self.phone;
    
    self.QQView = [[LTView alloc] initWithFrame:CGRectMake(10, 300, 350, 40)];
    [self.view addSubview:self.QQView];
    self.QQView.myLabel.text = @"QQ:";
    //    self.QQView.myTextField.text = self.QQ;
    
    self.addressView = [[LTView alloc] initWithFrame:CGRectMake(10, 360, 350, 40)];
    [self.view addSubview:self.addressView];
    self.addressView.myLabel.text = @"籍贯:";
    //    self.addressView.myTextField.text = self.address;
}


- (void)finish:(UIBarButtonItem *)item {
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    Student *stu = [[Student alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    stu.name = self.nameView.myTextField.text;
    stu.sex = self.sexView.myTextField.text;
    NSInteger number = self.numberView.myTextField.text.integerValue;
    stu.number = [NSNumber numberWithInteger:number];
    stu.phone = self.phoneView.myTextField.text;
    stu.qq = self.QQView.myTextField.text;
    stu.addressMap = self.addressView.myTextField.text;
    
    [self.manager saveContext];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
}



@end
